package com.hncj.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.hncj.dao.ImageMapper;
import com.hncj.pojo.Image;
import com.hncj.service.ImageService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
public class ImageServiceImpl extends ServiceImpl<ImageMapper, Image> implements ImageService {

    @Autowired
    private ImageMapper imageMapper;

    @Override
    @Transactional
    public List<Image> getNamesImage(String[] names) {

        LambdaQueryWrapper<Image> lambdaQueryWrapper = new LambdaQueryWrapper<>();

        for(int i = 0; i < names.length; i++){
            if(i==0) lambdaQueryWrapper.eq(Image::getImage,names[i]);
            else lambdaQueryWrapper.or().eq(Image::getImage,names[i]);
        }

        List<Image> images = imageMapper.selectList(lambdaQueryWrapper);
        System.out.println(images);
        return images;

    }

    @Override
    @Transactional
    public Boolean removeImages(String[] names) {
        LambdaQueryWrapper<Image> lambdaQueryWrapper = new LambdaQueryWrapper<>();

        for(int i = 0; i < names.length; i++){
            if(i==0) lambdaQueryWrapper.eq(Image::getImage,names[i]);
            else lambdaQueryWrapper.or().eq(Image::getImage,names[i]);
        }

        int delete = imageMapper.delete(lambdaQueryWrapper);

        return delete==names.length;
    }
}
